<?php
// +----------------------------------------------------------------------
// | 奇乐中介担保交易系统 免费版 2019
// +----------------------------------------------------------------------
// | 官方网址：http://www.qilecms.com   
// +----------------------------------------------------------------------
// | 产品文档：http://www.qilecms.com/doc
// +----------------------------------------------------------------------
// | 开发作者:奇乐CMS
// +----------------------------------------------------------------------
// | 版权说明：这不是一个自由软件！本产品仅限个人使用，但请务必保留底部版权，以免产生不必要的纠纷,
// | 您只能在不用于商业目的的前提下对程序代码进行修改和使用，
// | 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
// +----------------------------------------------------------------------
namespace app\user\controller;
class Forget extends Base{
// 第一步 接受验证码
  public function index(){
  	$param = $this->request->param();
     if($this->request->isAjax()){
         if($param['type'] == 1){
         	// 邮箱找回
         	  //表单验证
         	 $data =[];
           	$data['email'] = $param['email'];
           	$data['email_code'] = $param['email_code'];
             $rule =[
                'email'=>'require|email',
                'email_code'=>'require|number',
              ];
              $msg= [
               'email.require' =>'请输入邮箱',
               'email.email' =>'请输入正确的邮箱格式',  
               'email_code.require' =>'请输入验证码',
               'email_code.number' =>'请输入正确格式的邮箱验证码',
              ];
               $error = $this->checkSubmit($data,$rule,$msg);  
              if($error){
                  return error_json($error);
               }

           	$where['email'] = $param['email'];
           	$user = model('user')->infoData($where);
           	if(empty($user)){
                return error_json('邮箱不存在');
           	}
             if($param['email_code'] != session('email_code')){
                return error_json('验证码错误');
             }
             session('email',$data['email']);
             session('step',1);
            return success_json('成功'); 
   

         }elseif($param['type'] == 2){
              //手机短信找回
               $data =[];
               $data['mobile'] = $param['mobile'];
               $data['sms_code'] =$param['sms_code'];
               $rule =[
                  'mobile'   =>'require|mobile',
                  'sms_code' =>'require|number',
                ];
                $msg= [
                 'mobile.require'   =>'请输入手机号',
                 'mobile.mobile'     =>'请输入正确的手机号格式',
                 'sms_code.require' =>'请输入验证码',
                 'sms_code.number'  =>'请输入正确格式短信验证码',   
                ];

                 $error = $this->checkSubmit($data,$rule,$msg);  
                if($error){
                    return error_json($error);
                 }
                $where['mobile'] = $data['mobile'];
             	$user = model('user')->infoData($where);
             	if(empty($user)){
                 return error_json('手机号没有注册');
             	}

               if($param['sms_code'] != session('sms_code')){
                  return error_json('短信验证码错误');
               }  
               
               session('mobile',$data['mobile']);
               session('step',1);
              return success_json('成功'); 
         }

     }else{
        return $this->fetch();
     }
  	
  }

// 找回密码
  public function reset(){

		    $param = $this->request->param();
		    $password  =  $param['password'];   
        $repassword = $param['repassword'];
		    if($this->request->isAjax()){

           if(!$password){
             return error_json('密码不能为空');
            }
            if(! $repassword){
               return error_json('重复密码不能为空');
            }
            if($password !=  $repassword){
              return error_json('两次密码不一致');
            }

            if($param['type'] ==1){
//邮箱找回
       
              $map['email']  = session('email');
              $map['is_email']  = 1;
              $user = model('user')->infoData($map);
                     $data['password'] = password_key($user['username'],$password,$user['passsalt']);
                     $data['login_time'] = time();
                     $data['reg_time']   = time();
                    
                    
                     // 更新用户
                     if(model('user')->editData($data,$map)){
                        session('step',2);
                       return  success_json('成功');
                     }else{
                       return  error_json('密码不能与原来的相同');
                     }
               
            }elseif($param['type'] ==2){
// 手机找回
  
              $map['mobile']  = session('mobile');
              $map['is_mobile']  = 1;
              $user = model('user')->infoData($map);
                 $data['password'] = password_key($user['username'],$password,$user['passsalt']);
                     $data['login_time'] = time();
                     $data['reg_time']  = time();
                     // 更新用户
                     if(model('user')->editData($data,$map)){
                       session('step',2);
                       return success_json('成功');
                     }else{
                       return  error_json('密码不能与原来的相同');
                     }
                   
            }
         

		      }else{
              if(session('step') != 1){
                return $this->redirect('Forget/index');      
              }

              if(empty($param['type'])){
                $this->redirect('Forget/index'); 
              }

                if($param['type'] ==1){
                       $email =  session('email');
                        if(empty($email)){
                          $this->redirect('Forget/index'); 
                        
                        }
                }elseif($param['type'] ==2){
                           $mobile =  trim(session('mobile'));
                           if(empty($mobilel)){
                             return $this->redirect('Forget/index'); 
                          }
                }

              return $this->fetch();
          }
		    
	
  }
//  发送验证码
  public function sendcode(){
	     $param = $this->request->param();
         if($this->request->isAjax()){
            $rule =[
              'mobile'   =>'require|mobile',
            ];
            $msg= [
             'mobile.require'  =>'请输入手机号',
             'mobile.mobile'   =>'请输入正确的手机号',
            ];
             $error = $this->checkSubmit($param,$rule,$msg);  
            if($error){
                return error_json($error);
             } 


	     $mobile = $param['mobile'];
	     session('sms_code', null);
	     $rand =  rand(100000,900000);  //随机数
	     $where['mobile'] = $mobile;
	     $user = model('user')->infoData($where);
	    if(!$user){
	   	//手机号没有注册
           return error_json('手机号没有注册');
	     }

	       $params =['sendcode'=>$rand];  //六位数
	       $res = send_sms($mobile,'sms_yzm',$params,"aliyun");
	      if($res =='success'){
	         session('sms_code',$rand,'60');
	         return success_json('短信验证码发送成功');
	      }else{
	         return error_json($res);
	      }   
         }
	    
  }
//发送邮件
 public function sendEmail()
{
	$param = $this->request->param();
	if($this->request->isAjax()){
           $rule =[
              'email'   =>'require|email',
            ];
            $msg= [
             'email.require'  =>'请输入邮箱',
             'email.email'   =>'请输入正确的邮箱',
            ];
             $error = $this->checkSubmit($param,$rule,$msg);  
            if($error){
                return error_json($error);
             } 
            $rand = rand(100000,999999);
            session('email_code',$rand);
			$to_email = $param['email'];
			$subject  = '这是我的测试邮件';
			$body     = '您的验证码是：'.$rand.',有效时间:5分钟';
			$res = send_email($to_email,$subject,$body);
		  if($res === true){
		     return success_json('验证邮件发送成功');
		  }else{
		  	 return error_json($res);
		  }
	}else{
		return error_json('非法请求');
	}



}
// 找回成功
  public function success(){
  	if(session('step') != 2){
      return $this->redirect('Forget/index');      
  	}
   session('mobile',NULL);
   session('email',NULL);
   session('step',NULL);
   return $this->fetch();
  }

 // // 第一步的 核对验证码
 //  public function codeCheck(){
 //    $param = $this->request->param();

 //    if(!$param['smes_code']){ 
 //      return error_json('验证码不能为空');
 //    }
 //    if($param['smes_code'] != session('sms_code')){
 //      return error_json('验证码错误');
 //    }

 //        //检查手机是否已经注册
 //     $user = model('user')->getMemberByUsername($param['mobile']);
 //     if(!$user){
 //      session('mobile',$param['mobile']);
 //      session('nationality_code',$param['nationality_code']);
 //      return success_json('找回成功'); 
 //     }

    
 //  }


}